package com.og.vas.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.og.vas.common.exception.Assert;
import com.og.vas.common.result.ResponseEnum;
import com.og.vas.mapper.UserInfoMapper;
import com.og.vas.pojo.dto.VacInfoDTO;
import com.og.vas.pojo.entity.UserInfo;
import com.og.vas.pojo.entity.VaccinaInfo;
import com.og.vas.mapper.VaccinaInfoMapper;
import com.og.vas.pojo.query.VacInfoQuery;
import com.og.vas.pojo.vo.VacInfoListVO;
import com.og.vas.pojo.vo.VacInfoVO;
import com.og.vas.service.VaccinaInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xzy
 * @since 2022-01-11
 */
@Service
public class VaccinaInfoServiceImpl extends ServiceImpl<VaccinaInfoMapper, VaccinaInfo> implements VaccinaInfoService {

    @Autowired
    UserInfoMapper userInfoMapper;

    @Override
    public void addByUid(Integer uid, VacInfoDTO vacInfoDTO) {

        VaccinaInfo vaccinaInfo = new VaccinaInfo();
        BeanUtils.copyProperties(vacInfoDTO, vaccinaInfo);
        vaccinaInfo.setUid(uid);

        //校验一个剂次一个人只能接种一次
        QueryWrapper<VaccinaInfo> Wrapper = new QueryWrapper<>();
        Wrapper.eq("uid",uid).
                eq("dose",vacInfoDTO.getDose());
        Assert.isTrue(baseMapper.selectCount(Wrapper)==0, ResponseEnum.ABNORMAL_PARA);

        baseMapper.insert(vaccinaInfo);
    }

    @Override
    public void addByIdCardNum(String Num, VacInfoDTO vacInfoDTO) {
        addByUid(getUidHelper("id_card_num",Num),vacInfoDTO);

    }

    @Override
    public void addByUserName(String userName, VacInfoDTO vacInfoDTO) {
        addByUid(getUidHelper("username",userName),vacInfoDTO);
    }

    @Override
    public void addByPhone(String phone, VacInfoDTO vacInfoDTO) {
        addByUid(getUidHelper("phone",phone),vacInfoDTO);
    }

    @Override
    public void deleteByUid(Integer uid) {
        QueryWrapper<VaccinaInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",uid);
        baseMapper.delete(wrapper);
    }

    @Override
    public void deleteByid(Integer id) {
        baseMapper.deleteById(id);
    }

    @Override
    public void deleteIdCardNum(String Num) {
        deleteByUid(getUidHelper("id_card_num",Num));
    }

    @Override
    public void deleteByUserName(String userName) {
            deleteByUid(getUidHelper("username",userName));
    }

    @Override
    public void deleteByPhone(String phone) {
        deleteByid(getUidHelper("phone",phone));
    }




    private Integer getUidHelper(String column,String value){

        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq(column,value);
        UserInfo userInfo = userInfoMapper.selectOne(wrapper);
        //断言对象不为空
        Assert.notNull(userInfo,ResponseEnum.LOGIN_MOBILE_ERROR);
       return userInfoMapper.selectOne(wrapper).getId();
    }




    @Override
    public List<VacInfoVO> getByid(Integer uid) {

        QueryWrapper<VaccinaInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",uid);

        List<VaccinaInfo> infos = baseMapper.selectList(wrapper);

        //将info映射为vo
        Stream<VacInfoVO> stream = infos.stream().map(VaccinaInfo::toVo);
        List<VacInfoVO> collect = stream.collect(Collectors.toList());


        return collect;
    }
    @Override
    public IPage<VacInfoListVO> list(Page<VacInfoListVO> page, VacInfoQuery query) {
        return baseMapper.listByquery(page,query);
    }

    @Override
    public void update(Integer id, VacInfoDTO vacInfoDTO) {
        VaccinaInfo vaccinaInfo = baseMapper.selectById(id);
        BeanUtils.copyProperties(vacInfoDTO,vaccinaInfo);
        baseMapper.updateById(vaccinaInfo);
    }
}
